package com.cxl.demo.mapper;

import com.cxl.demo.vo.Record;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface RecordMapper {
    @Select("select * from record where assigmentId =#{assigmentId} limit #{page},#{size}")
    @Results(value={
            @Result(property = "recordId",column = "recordId"),
            @Result(property = "assigmentId",column = "assigmentId"),
            @Result(property = "getRecordUserId",column = "getRecordUserId"),
            @Result(property = "recordCondition",column = "recordCondition"),
            @Result(property = "recordGetTime",column = "recordGetTime"),
            @Result(property = "recordEndTime",column = "recordEndTime"),
            @Result(property = "recordGrade",column = "recordGrade"),
            @Result(property = "user",column = "getRecordUserId",
                    one =@One(select = "com.cxl.demo.mapper.UserMapper.findUserInfoByUserId")),
            @Result(property = "recordGradeVal",column = "recordGrade",
                    one =@One(select = "com.cxl.demo.mapper.RecordGradeMapper.getGradeById")),
            @Result(property = "recordConditionVal",column = "recordCondition",
                    one =@One(select = "com.cxl.demo.mapper.RecordConditionMapper.getConditionById")),
            @Result(property = "assigment",column = "assigmentId",
                    one =@One(select = "com.cxl.demo.mapper.AssigmentMapper.findAssigmentById"))
    })

    List<Record> getRecordsByAssigmentId(Map<String,Object> map);
    @Select("select count(1) from record where assigmentId =#{assigmentId} ")
    Integer getRecordsByAssigmentIdNum(Integer assigmentId);


    @Select("select * from record where getRecordUserId =#{userId} limit #{page},#{size}")
    @Results(value={
            @Result(property = "recordId",column = "recordId"),
            @Result(property = "assigmentId",column = "assigmentId"),
            @Result(property = "getRecordUserId",column = "getRecordUserId"),
            @Result(property = "recordCondition",column = "recordCondition"),
            @Result(property = "recordGetTime",column = "recordGetTime"),
            @Result(property = "recordEndTime",column = "recordEndTime"),
            @Result(property = "recordGrade",column = "recordGrade"),
            @Result(property = "user",column = "getRecordUserId",
                    one =@One(select = "com.cxl.demo.mapper.UserMapper.findUserInfoByUserId")),
            @Result(property = "recordGradeVal",column = "recordGrade",
                    one =@One(select = "com.cxl.demo.mapper.RecordGradeMapper.getGradeById")),
            @Result(property = "recordConditionVal",column = "recordCondition",
                    one =@One(select = "com.cxl.demo.mapper.RecordConditionMapper.getConditionById")),
            @Result(property = "assigment",column = "assigmentId",
                    one =@One(select = "com.cxl.demo.mapper.AssigmentMapper.findAssigmentById"))
    })
    List<Record> getRecordsByUserId(Map<String,Object> map);

    @Select("select count(1) from record where getRecordUserId =#{userId} ")
    Integer getRecordsByUserIdNum(Integer userId);




    @Select("select * from record where recordId =#{recordId}")
    Record getRecordByRecordId(Integer recordId);

    @Insert("insert into record (assigmentId,getRecordUserId,recordCondition,recordGetTime,recordEndTime,recordGrade)" +
            "values(#{assigmentId},#{getRecordUserId},#{recordCondition},#{recordGetTime},#{recordEndTime},#{recordGrade})")
    @Options(useGeneratedKeys = true ,keyProperty = "recordId",keyColumn = "recordId")
    Integer addRecord(Record record);

    @Delete("delete from record where recordId=#{recordId}")
    Integer delRecord(Integer recordId);



    Integer updateRecord(Record record);



    @Select("select count(1) from record where getRecordUserId=#{getRecordUserId}and assigmentId=#{assigmentId}")
    Integer  getIsRecord(Record record);


    @Select("select count(1) from record where assigmentId=#{assigmentId}")
    Integer recordNumberByAssigmentId(Integer assigmentId);

}
